package com.zs;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

/**
 * Description:
 * 使用  Topic Exchange   (主题转发)
 * @author: zsq-1186
 * Date: 2017-08-18-12:09
 */
public class EmitLogTopic {
    private static final String EXCHANGE_NAME = "ex_logs_topic";
    private static final String[] routing_keys  = { "kernal.info", "cron.warning", "auth.info", "kernel.critical" };

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("guest");

        Connection connection=factory.newConnection();
        Channel channel=connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"topic");

        for(int i=0;i<3;i++){
            String route_key=routing_keys[i];
            String message = route_key + "_log :" + UUID.randomUUID().toString();
            channel.basicPublish(EXCHANGE_NAME,route_key,null,message.getBytes());
        }
        connection.close();
        channel.close();

    }
}
